package com.techshinogongjian.fp.util;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
/**
 * 和编码解码相关的工具方法集合
 * 
 */
import android.util.Base64;

public class FpCoder {

	public static byte[] Int2Byte(int nVal) {
		byte[] targets = new byte[4];
		targets[3] = (byte) (nVal & 0x000000ff);// 最低位
		targets[2] = (byte) ((nVal >> 8) & 0x000000ff);// 次低位
		targets[1] = (byte) ((nVal >> 16) & 0x000000ff);// 次高位
		targets[0] = (byte) (nVal >>> 24 & 0x000000ff);// 最高位,无符号右移。
		return targets;
	}

	public static byte[] intTo4Byte(int n) {
		byte[] bytes = new byte[4];
		bytes[3] = (byte) ((n >> 24) & 0x000000ff);
		bytes[2] = (byte) ((n >> 16) & 0x000000ff);
		bytes[1] = (byte) ((n >> 8) & 0x000000ff);
		bytes[0] = (byte) ((n >> 0) & 0x000000ff);
		return bytes;
	}
	
	public static byte[] intTo2Byte(int n) {
		byte[] bytes = new byte[2];
		bytes[0] = (byte) ((n >> 0) & 0x000000ff);
		bytes[1] = (byte) ((n >> 8) & 0x000000ff);
		return bytes;
	}
	
	public static byte[] intTo4ByteBig(int n) {
		byte[] bytes = new byte[4];
		bytes[0] = (byte) ((n >> 24) & 0x000000ff);
		bytes[1] = (byte) ((n >> 16) & 0x000000ff);
		bytes[2] = (byte) ((n >> 8) & 0x000000ff);
		bytes[3] = (byte) ((n >> 0) & 0x000000ff);
		return bytes;
	}

	public static int Byte2Int(byte[] res) {
		int targets = (res[10] & 0xff) | ((res[9] << 8) & 0xff00);
		return targets;
	}

	public static int twoByteToInt(byte[] bytes) {
		int nu = (bytes[1] << 8 & 0xff00) + (bytes[0] & 0xff);
		return nu;
	}

	/**
	 * 小端
	 * 
	 * @param bytes
	 * @return
	 */
	public static int fourByteToInt(byte[] bytes) {
		int nu = (bytes[3] << 24 & 0xff000000) + (bytes[2] << 16 & 0xff0000)
				+ (bytes[1] << 8 & 0xff00) + (bytes[0] & 0xff);
		return nu;
	}

	public static int XorChk(byte[] bufData, int nStartPos, int nDatLen) {
		int nChkVal = 0;
		int i;
		for (i = 0; i < nDatLen; i++) {
			nChkVal = (nChkVal ^ (bufData[nStartPos + i] & 0xFF));
		}
		byte[] bChks = new byte[2];
		bChks[0] = (byte) (nChkVal & 0x000000FF);
		return nChkVal;
	}

	// bin to x30
	public static byte[] EncodeX30(byte[] bufData, int nStartPos, int nDatLen) {
		byte bRet[] = new byte[nDatLen * 2];
		int i;
		for (i = 0; i < nDatLen; i++) {
			bRet[i * 2] = (byte) (bufData[nStartPos + i] >>> 4 & 0x0F | 0x30);
			bRet[i * 2 + 1] = (byte) (bufData[nStartPos + i] & 0x0F | 0x30);
		}
		return bRet;
	}

	// x30 to bin
	public static byte[] DecodeX30(byte[] bufData, int nStartPos, int nDatLen) {
		byte bRet[] = new byte[nDatLen / 2];
		int i;
		for (i = 0; i < nDatLen / 2; i++) {
			bRet[i] = (byte) (((int) (bufData[nStartPos + 2 * i] & 0x0F) << 4)
					& ((int) (bufData[nStartPos + 2 * i + 1] & 0x0F)) & 0x000000FF);
		}
		return bRet;
	}

	public static byte[] EncodeB64(byte[] bArray, int nArrayLen) {
		return Base64.encode(bArray, 0, nArrayLen, Base64.NO_WRAP);
	}

	public static byte[] DecodeB64(String szB64Val, int nArrayLen) {
		byte[] bRet = Base64.decode(szB64Val, Base64.NO_WRAP);
		return bRet;
	}

	public static String byte2String(byte nByte) {
		byte[] bTemp = new byte[1];
		bTemp[0] = nByte;
		return byte2String(bTemp, 0, 1);
	}

	public static String byte2String(byte[] bBuf, int nStartPos, int nDatLen) {
		String szTemp;
		szTemp = "";
		for (int i = nStartPos; i < (nStartPos + nDatLen); i++) {
			String szOne;
			if (bBuf[i] < 0) {
				szOne = Integer.toHexString((int) (bBuf[i] + 256));
			} else {
				szOne = Integer.toHexString((int) bBuf[i]);
			}
			if (szOne.length() == 1) {
				szOne = "0" + szOne;
			}
			szTemp += szOne + "";
		}
		return szTemp;
	}

	public FpCoder() {
		// TODO Auto-generated constructor stub
	}

	public static Bitmap getRedTransparentBitmap(Bitmap bmFinger) {
		// Bitmap bmFinger1 = scaleBitmap(bmFinger, width);
		Bitmap sourceImg = bmFinger.copy(Config.ARGB_8888, true);
		int[] argb = new int[sourceImg.getWidth() * sourceImg.getHeight()];

		sourceImg.getPixels(argb, 0, sourceImg.getWidth(), 0, 0,
				sourceImg.getWidth(), sourceImg.getHeight());// 获得图片的ARGB值

		// number = number * 255 / 100;

		for (int i = 0; i < argb.length; i++) {
			if (((argb[i] & 0x000000FF) > 0x00000096)
					|| (argb[i] & 0x0000FF00) > 0x00009600) {
				argb[i] = (0 << 24) | (argb[i] & 0x00FF0000);
			} else {
				// argb[i] = (0xff << 24) | (argb[i] & 0x00FFFFFF);//原图红
				argb[i] = (0xff << 24) | (argb[i] & 0x00FF0000);// 这里改为了纯红
			}
		}

		sourceImg = Bitmap.createBitmap(argb, sourceImg.getWidth(), sourceImg

		.getHeight(), Config.ARGB_8888);

		return sourceImg;
	}
}
